package com.simple.datax.flow.generator;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.flow.constants.ProcessResultConstants;
import com.simple.datax.flow.message.EcFlowMessage;
import com.simple.datax.flow.message.EcFlowRespMessage;
import com.simple.datax.flow.message.EcFlowRespMessageBody;
import com.simple.datax.flow.message.EcFlowRespMessageHead;
import com.simple.datax.flow.util.DateUtil;

/** 
 * @Title: MessageGenerator.java 
 * @Package com.simple.datax.flow.generator 
 * @Description: TODO(报文生成器接口) 
 * @author tonney 
 * @date 2013-1-9 下午04:14:13 
 * @version V1.0 
 */
public class MessageGenerator {
	public static Logger log = LoggerFactory.getLogger(MessageGenerator.class);

	/**
	 * 根据原始流程报文和错误信息生成失败响应报文
	 * @param ecFlowMessage
	 * @param resultValue
	 * @return
	 */
	public static EcFlowRespMessage generatFailureRespMessage(EcFlowMessage ecFlowMessage,int resultValue){
		EcFlowRespMessage flowRespMessage = null;
		try{
			if(ecFlowMessage!=null){
				String taskDetail = "任务ID【"+ecFlowMessage.getFlowMessageBody().getTaskId()+"】";
				flowRespMessage = new EcFlowRespMessage();
				EcFlowRespMessageHead flowRespHead = flowRespMessage.addEcFlowRespMessageHead();
				flowRespHead.setMessageId(ecFlowMessage.getFlowMessageHead().getMessageId());
				flowRespHead.setMessageTime(DateUtil.getCurTime());
				flowRespHead.setMessageType(ecFlowMessage.getFlowMessageHead().getMessageType());
				flowRespHead.setSenderAddress(ecFlowMessage.getFlowMessageHead().getReceiverAddress());
				flowRespHead.setSenderId(ecFlowMessage.getFlowMessageHead().getReceiverId());
				flowRespHead.setReceiverAddress(ecFlowMessage.getFlowMessageHead().getSenderAddress());
				flowRespHead.setReceiverId(ecFlowMessage.getFlowMessageHead().getSenderId());
				EcFlowRespMessageBody flowRespBody = flowRespMessage.addEcFlowRespMessageBody();
				EcFlowRespMessageBody.ECFlowResponseHead respHead = flowRespBody.new ECFlowResponseHead(); 
				respHead.setBpNo(ecFlowMessage.getFlowMessageBody().getFlowId());
				respHead.setCorpTaskId(ecFlowMessage.getFlowMessageBody().getCorpTaskId());
				respHead.setMessageId(ecFlowMessage.getFlowMessageBody().getMessageId());
				respHead.setRequestMessageId(ecFlowMessage.getFlowMessageBody().getMessageId());
				respHead.setResultCode(""+ecFlowMessage.getFlowMessageBody().getResultCode());
				respHead.setResultValue(ecFlowMessage.getFlowMessageBody().getResultValue());
				respHead.setTaskId(ecFlowMessage.getFlowMessageBody().getTaskId());
				EcFlowRespMessageBody.ResponseActionResult actionResult = flowRespBody.new ResponseActionResult();
				actionResult.setActionId(ecFlowMessage.getFlowMessageBody().getActivityIdList().get(0));
				actionResult.setResultCode(""+resultValue);
				actionResult.setResultValue(taskDetail+ProcessResultConstants.getProcDetail(resultValue));
				flowRespBody.getActionResultList().add(actionResult);
				flowRespBody.setEcFlowResponseHead(respHead);
			}
		}catch(Exception e){
			log.error("MessageGenerator::generatFailureRespMessage error=>"+e.toString());
		}
		return flowRespMessage;
	}
}
